package com.ruoyi.ade.llm.agent;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.ade.llm.model.ChatRequest;
import com.ruoyi.ade.llm.model.ChatResponse;
import com.ruoyi.ade.llm.model.OpenAiChatResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * API地址：https://chatanywhere.apifox.cn/api-92222076
 */
public class OpenAIAgent extends  LLMAgent{

    private final String api_url = "";
    private final String apikey_free = "sk-fnr5j20WyD1cRzEo7jLIlU0B3moL8Gu7tIlI3SxnJJnvYahV";
    private final String apikey_paid = "sk-MWFB9RCZ8NnqE0ToQ3w4yH6g6HtHvvqpyDqksnAhB8eJCJRV";

    public static final String model_gpt35_turbo = "gpt-3.5-turbo";;
    public static final String model_gpt4o_mini = "gpt-4o-mini";
    public static final String model_gpt4 = "gpt-4";
    public static final String model_gpt4o = "gpt-4o";

    private final String chat_url = "https://api.chatanywhere.tech/v1/chat/completions";

    private OpenAIAgent(){}

    private static class SingletonHelper{
        private static OpenAIAgent INSTANCE = new OpenAIAgent();
    }
    public static OpenAIAgent getInstance(){
        return SingletonHelper.INSTANCE;
    }


    public ChatResponse chat(ChatRequest request){
        String json = post(chat_url, JSONObject.toJSONString(request));
        if(StringUtils.isNoneEmpty(json)) {
            OpenAiChatResponse resp =  JSONObject.parseObject(json, OpenAiChatResponse.class);
            ChatResponse response = new ChatResponse();
            response.setModel(resp.getModel());
            response.setMessage(resp.getChoices().get(0).getMessage());
            return response;
        }
        return null;
    }

//    public OpenAiChatResponse chat(ChatRequest request){
//        String json = post(chat_url, JSONObject.toJSONString(request));
//        if(StringUtils.isNoneEmpty(json))
//            return JSONObject.parseObject(json, OpenAiChatResponse.class);
//        return null;
//    }

    private String post(String url, String jsonRequest){
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost postRequest = new HttpPost(url);
            StringEntity entity = new StringEntity(jsonRequest, "UTF-8");
//            entity.setContentType("application/json");
            postRequest.setEntity(entity);
            postRequest.setHeader("Authorization","Bearer "+apikey_paid);
            postRequest.setHeader("Content-Type","application/json");
            CloseableHttpResponse resp = httpClient.execute(postRequest);
            HttpEntity e = resp.getEntity();
//            System.out.println(JSONObject.toJSONString(resp));
            if(e!=null && resp.getStatusLine().getStatusCode() == 200){
                String content = EntityUtils.toString(e);
              return content;
            }
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }



    public static void main(String[] args) {
        OpenAIAgent openAIClient = new OpenAIAgent();
        ChatRequest request = new ChatRequest("gpt-4o","You are a helpful assistant.","Hello!");
        System.out.println(JSONObject.toJSONString(request));
        ChatResponse response = openAIClient.chat(request);

        System.out.println(JSONObject.toJSONString(response));
        System.out.println(response.getMessage().getContent());
        System.out.println(response.getModel());
    }

}
